<template>
  <a-modal
    :title="model && model.id > 0 ? '分配会员' : '分配会员'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="员工">
          <!-- <a-input v-decorator="['assignFrom']" disabled="true" /> -->
          <a-select v-decorator="['currentEmpId']" placeholder="请选择" disabled>
            <a-select-option :value="i.id" v-for="i in useroption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="分配给">
          <a-select v-decorator="['assignEmpId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in useroption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="会员数">
          <a-input v-decorator="['currentNum']" disabled="true" placeholder="请输入会员数"/>
        </a-form-item>
        <a-form-item label="分配值">
          <a-input addon-after="个" v-decorator="['assignNum']" placeholder="请输入分配值"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getEmployeesListUsingPOST } from '@/api/api/yuangongguanli.ts'
import pick from 'lodash.pick'

// 表单字段
const fields = ['currentEmpId', 'assignEmpId','currentNum','assignNum','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
      useroption: [],
    }
  },
  mounted() {
    this.getUserOption()
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    getUserOption() {
      getEmployeesListUsingPOST(this.optionParam)
        .then((res) => {
          this.useroption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  }
}
</script>
